/*package com.ultrasoft.farmasoft.web;

public class PortadaDepartmentsController {

}*/

package com.ultrasoft.farmasoft.web;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
//import com.ultrasoft.farmasoft.service.GeneralAdministrationService;
import com.ultrasoft.farmasoft.service.PortadaDepartmentsService;
import com.ultrasoft.farmasoft.util.UtilCommon;
import com.ultrasoft.farmasoft.util.UtilConverter;
import com.ultrasoft.farmasoft.util.UtilUseFulTree;
import com.ultrasoft.farmasoft.domain.Country;
import com.ultrasoft.farmasoft.domain.Region;
import com.ultrasoft.farmasoft.domain.Article;
import com.ultrasoft.farmasoft.domain.Section;
import com.ultrasoft.farmasoft.domain.User;

@Controller
@SessionAttributes({"s_UserPharmacy"})
@RequestMapping(value="/PortadaDepartments")
public class PortadaDepartmentsController {
	
	private final PortadaDepartmentsService portadaDepartmentsService;
	//private HttpSession session;
	//private Userpharmacy s_UserPharmacy;
	//private int s_userId;
	//private int s_pharmacyId;

	@Autowired
    public PortadaDepartmentsController(PortadaDepartmentsService portadaDepartmentsService) {
        this.portadaDepartmentsService = portadaDepartmentsService;
    }
	
	//start esencial
	private String redirectHumanResources(HttpServletRequest request){
		return "redirect:/configActivePrinciple?"+UtilCommon.menuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.menuId_)+"&"+UtilCommon.subMenuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.subMenuId_); 
	}
	
	/*private boolean sessionControl(HttpServletRequest request){
		boolean res = false;
		session = request.getSession(true);
		if(session.getAttribute("s_UserPharmacy") != null){
			s_UserPharmacy 	= (Userpharmacy)session.getAttribute("s_UserPharmacy");
			s_userId 		= s_UserPharmacy.getUser().getUserId();
			s_pharmacyId 	= s_UserPharmacy.getPharmacy().getPharmacyId();
			res = true;
		}
		return res;
	}*/
	
	private String handleRedirectHumanResources(HttpServletRequest request,String headMainList) {
    	try {
    		return "redirect:/"+headMainList+UtilCommon.menuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.menuId_)+"&"+UtilCommon.subMenuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.subMenuId_); 
		}catch (Exception e) {
			return redirectHumanResources(request);
		}
    }
	//Control main page ADMINGENERAL
	/*@RequestMapping(value="/homeDepartamemts",method = RequestMethod.GET)
    protected String homeDepartamts(HttpServletRequest request,Model model, Region id) throws ServletException {
		//session = request.getSession();
    	//if((Userpharmacy)session.getAttribute("s_UserPharmacy") == null){
    		
    		List<Article> articulos = portadaDepartmentsService.getArticleList(); // para mapear los datos
			model.addAttribute("articulos", articulos);
    		User user = new User();
        	model.addAttribute("user",user);
        	return UtilCommon.pHomeDepartments+"homeDepartamts";
        	
        	//Country c=generalAdminService.searchCountryByID(idCountry);
        	//Region r=portadaDepartmentsService.searchArticleByID(idArticle);
        	
        	//List<Article> art = portadaDepartmentsService.getArticleListWithIdRegionActive(idRegion, true);
        	
    	//}else{
    		//return "redirect:/mainFront?";	
    	//}	
		
    }*/
	
	@RequestMapping(value="/homeDepartamemts",method = RequestMethod.GET)
    protected String homeDepartamts(HttpServletRequest request,Model model) throws ServletException {
		
		System.out.println(request.getParameterMap());
		
		//int $valores = array_values($_GET);
		//session = request.getSession();
    	//if((Userpharmacy)session.getAttribute("s_UserPharmacy") == null){
    		
		List<Article> articulos = portadaDepartmentsService.getArticleList();
		model.addAttribute("articulos", articulos);
		//int dato=articulos.size();
		//System.out.println("ladyyyyyyyyyy"+dato);
		
        return UtilCommon.pHomeDepartments+"homeDepartamts";																				
    	//}else{
    		//return "redirect:/mainFront?";	
    	//}	
		
    }
	
	/* Add sub-menu the Administration General COUNTRY*/
	/*@RequestMapping(value="/adminCountry", method=RequestMethod.GET)
	public String handleRequestAdminHumanResources(SessionStatus status,HttpServletRequest request, Model model)throws ServletException  {
		if(this.sessionControl(request)){
			//List<Country> contries = generalAdminService.getCountryList();
			model.addAttribute("contries",contries);
			return UtilCommon.fCountry+"adminCountry";
	    }
		else 
			return redirectHumanResources(request);
	}*/
	
	/*
	 * COUNTRY
	 */
	// Method add and edit adminCountry
	 /*@RequestMapping(value = "/generalAdministrationAddEditCountry", method = RequestMethod.POST)
	 public @ResponseBody String handleRequestGeneralAdministrationAddEditCountry(@RequestParam short idCountry,@RequestParam Integer accion,@RequestParam String name,@RequestParam String sigla){
	    String msg="";
		if(accion == 0){
			msg="Se agrego ";
			generalAdminService.addCountry(new Country(idCountry, name, sigla));
		}
		if(accion == 1){
		    msg="Se Edito ";
		    Country c=generalAdminService.searchCountryByID(idCountry);
		    c.setName(name);
		    c.setInitials(sigla);
		    generalAdminService.updateCountry(c);
		}
		return msg+"correctamente!!!";
	}
	//remove country
	 @RequestMapping(value = "/generalAdministrationRemoveCountry", method = RequestMethod.GET)
	 public @ResponseBody String handleGeneralAdministrationRemoveCountry(@RequestParam short idCountry,@RequestParam String name, HttpServletRequest request){
		 System.out.println(request.getParameterMap());
		 generalAdminService.removeCountry(generalAdminService.searchCountryByID(idCountry));
		 return "El Pais "+name+" se Elimino correctamente!!!";
	 }*/
}
